//
//  KATStandardTablefooter.m
//  KATFramework
//
//  Created by Kantice on 16/7/19.
//  Copyright © 2016年 KatApp. All rights reserved.
//

#import "KATStandardTableFooter.h"

@implementation KATStandardTableFooter


#pragma -mark 类方法

//获取实例
+ (instancetype)tableFooterWithHeight:(float)height andScrollView:(UIScrollView *)scrollView
{
    if(!scrollView)
    {
        return nil;
    }
    
    KATStandardTableFooter *footer=[[[self alloc] init] autorelease];
    
    [footer initWithHeight:height andScrollView:scrollView];
    footer.stateDelegate=footer;
    
    footer.hasTite=YES;
    footer.hasPushingIndicator=YES;
    footer.hasLoadingIndicator=YES;
    
    footer.infoPushing=@"push to load!";
    footer.infoRelease=@"release to load!";
    footer.infoLoading=@"loading...";
    
    footer.animationPushing=ST_FOOTER_ANIM_STROKE_END;
    footer.animationLoading=ST_FOOTER_ANIM_ROTATE;
    footer.animationPushingDuration=0.24;
    footer.animationLoadingDuration=2.0;
    footer.animationPushingRotateStartValue=0.0;
    footer.animationPushingRotateEndValue=0.5;
    footer.animationPushingScaleXStartValue=0.0;
    footer.animationPushingScaleXEndValue=1.0;
    footer.animationPushingScaleYStartValue=0.0;
    footer.animationPushingScaleYEndValue=1.0;
    footer.animationPushingStrokeStartStartValue=1.0;
    footer.animationPushingStrokeStartEndValue=0.0;
    footer.animationPushingStrokeEndStartValue=0.0;
    footer.animationPushingStrokeEndEndValue=1.0;
    footer.animationPushingAlphaStartValue=0.0;
    footer.animationPushingAlphaEndValue=1.0;
    footer.animationLoadingRotateStartValue=0.0;
    footer.animationLoadingRotateEndValue=1.0;
    footer.animationLoadingScaleXStartValue=0.0;
    footer.animationLoadingScaleXEndValue=1.0;
    footer.animationLoadingScaleYStartValue=0.0;
    footer.animationLoadingScaleYEndValue=1.0;
    footer.animationLoadingStrokeStartStartValue=1.0;
    footer.animationLoadingStrokeStartEndValue=0.0;
    footer.animationLoadingStrokeEndStartValue=0.0;
    footer.animationLoadingStrokeEndEndValue=1.0;
    footer.animationLoadingAlphaStartValue=0.0;
    footer.animationLoadingAlphaEndValue=1.0;
    footer.isAnimationLoadingAutoReverse=NO;
    footer.isAnimationPushingActAtRelease=NO;
    
    footer.indicatorTypePushing=SHAPE_DAISY;
    footer.indicatorTypeLoading=SHAPE_DAISY;
    
    return footer;
}


#pragma -mark 对象方法


//初始化
- (void)initFooter
{
    //标题
    self.title=[[[UILabel alloc] init] autorelease];
    _title.frame=CGRectMake(self.bounds.size.height*1.0, self.bounds.size.height*0.1, self.bounds.size.width-self.bounds.size.height*2.0, self.bounds.size.height*0.8);
    _title.text=_infoPushing;
    _title.textColor=KCOLOR_DARK;
    _title.textAlignment=NSTextAlignmentCenter;
    _title.numberOfLines=0;
    _title.font=[UIFont systemFontOfSize:self.bounds.size.height*0.28];
    _title.backgroundColor=[UIColor clearColor];
    [self addSubview:_title];
    
    
    //指示器
    
    if(![KATShapeUtil hasShapeType:_indicatorTypePushing])
    {
        _indicatorTypePushing=SHAPE_DAISY;
    }
    
    self.indicatorPushing=[KATShapeUtil shapeWithType:_indicatorTypePushing andWidth:self.bounds.size.height*0.5 andHeight:self.bounds.size.height*0.5];
    _indicatorPushing.lineWidth=1.0;
    _indicatorPushing.strokeColor=KCOLOR_BLUE.CGColor;
    _indicatorPushing.animationTimingMode=kCAMediaTimingFunctionLinear;
    _indicatorPushing.position=CGPointMake(self.bounds.size.height*0.5, self.bounds.size.height*0.5);
    [self.layer addSublayer:_indicatorPushing];
    
    
    if(![KATShapeUtil hasShapeType:_indicatorTypeLoading])
    {
        _indicatorTypeLoading=SHAPE_DAISY;
    }
    
    self.indicatorLoading=[KATShapeUtil shapeWithType:_indicatorTypeLoading andWidth:self.bounds.size.height*0.5 andHeight:self.bounds.size.height*0.5];
    _indicatorLoading.lineWidth=1.0;
    _indicatorLoading.strokeColor=KCOLOR_BLUE.CGColor;
    _indicatorLoading.animationTimingMode=kCAMediaTimingFunctionLinear;
    _indicatorLoading.position=CGPointMake(self.bounds.size.height*0.5, self.bounds.size.height*0.5);
    [self.layer addSublayer:_indicatorLoading];
    
    
    //提示
    self.tips=[KATToast toast];
    _tips.position=TOAST_POSITION_BOTTOM;
    _tips.duration=TOAST_DURATION_SHORT;
    _tips.canTap=YES;
    
    
    //指示器状态初始化
    [self restoreIndicators];
    
}


//加载完成
- (void)finishLoadWithMessage:(NSString *)message
{
    [self restoreIndicators];
    
    [self finishLoad];
    
    if(_hasPushingIndicator)
    {
        _indicatorPushing.hidden=NO;
    }
    
    _title.text=_infoPushing;
    
    if(message)//显示toast
    {
        _tips.text=message;
        [_tips show];
    }
}


//内部函数：恢复指示器
- (void)restoreIndicators
{
    [_indicatorPushing clearAnimations];
    [_indicatorPushing restoreShape];
    [_indicatorLoading clearAnimations];
    [_indicatorLoading restoreShape];
    
    _indicatorPushing.hidden=YES;
    _indicatorLoading.hidden=YES;
    
    if(!_hasTite)
    {
        _title.hidden=YES;
    }
    else
    {
        _title.hidden=NO;
    }
    
    
    //上推动画
    if(_animationPushing/ST_FOOTER_ANIM_ROTATE%10>0)//旋转动画
    {
        [_indicatorPushing transformRotateToAngle:_animationPushingRotateStartValue*M_PI*2];
    }
    
    if(_animationPushing/ST_FOOTER_ANIM_SCALE_X%10>0 || _animationPushing/ST_FOOTER_ANIM_SCALE_Y%10>0)//缩放动画
    {
        float w=(_animationPushing/ST_FOOTER_ANIM_SCALE_X%10>0?_animationPushingScaleXStartValue:1.0);
        float h=(_animationPushing/ST_FOOTER_ANIM_SCALE_Y%10>0?_animationPushingScaleYStartValue:1.0);
        
        [_indicatorPushing transformScaleToWitdh:w andHeight:h];
    }
    
    if(_animationPushing/ST_FOOTER_ANIM_STROKE_START%10>0)//初始点动画
    {
        _indicatorPushing.strokeStart=_animationPushingStrokeStartStartValue;
    }
    
    if(_animationPushing/ST_FOOTER_ANIM_STROKE_END%10>0)//结束点动画
    {
        _indicatorPushing.strokeEnd=_animationPushingStrokeEndStartValue;
    }
    
    if(_animationPushing/ST_FOOTER_ANIM_ALPHA%10>0)//透明度动画
    {
        _indicatorPushing.opacity=_animationPushingAlphaStartValue;
    }
    
    //加载动画
    if(_animationLoading/ST_FOOTER_ANIM_ROTATE%10>0)//旋转动画
    {
        [_indicatorLoading transformRotateToAngle:_animationLoadingRotateStartValue*M_PI*2];
    }
    
    if(_animationLoading/ST_FOOTER_ANIM_SCALE_X%10>0 || _animationLoading/ST_FOOTER_ANIM_SCALE_Y%10>0)//缩放动画
    {
        float w=(_animationLoading/ST_FOOTER_ANIM_SCALE_X%10>0?_animationLoadingRotateStartValue:1.0);
        float h=(_animationLoading/ST_FOOTER_ANIM_SCALE_Y%10>0?_animationLoadingRotateStartValue:1.0);
        
        [_indicatorLoading transformScaleToWitdh:w andHeight:h];
    }
    
    if(_animationLoading/ST_FOOTER_ANIM_STROKE_START%10>0)//初始点动画
    {
        _indicatorLoading.strokeStart=_animationLoadingStrokeStartStartValue;
    }
    
    if(_animationLoading/ST_FOOTER_ANIM_STROKE_END%10>0)//结束点动画
    {
        _indicatorLoading.strokeEnd=_animationLoadingStrokeEndStartValue;
    }
    
    if(_animationLoading/ST_FOOTER_ANIM_ALPHA%10>0)//透明度动画
    {
        _indicatorLoading.opacity=_animationLoadingAlphaStartValue;
    }
    
    //    [_tips hide];
}


#pragma -mark 回调方法

//上推
- (void)footerPushWithProgress:(float)progress
{
    if(_hasPushingIndicator && self.state!=TFOOTER_STATE_LOADING && self.state!=TFOOTER_STATE_FINISH)
    {
        _indicatorPushing.hidden=NO;
    }
    
    //更改标题内容
    if(progress>=1.0)
    {
        if(self.state!=TFOOTER_STATE_RELEASE && self.state!=TFOOTER_STATE_LOADING && self.state!=TFOOTER_STATE_FINISH)
        {
            _title.text=_infoRelease;
        }
    }
    else
    {
        if(self.state!=TFOOTER_STATE_PUSHING && self.state!=TFOOTER_STATE_LOADING && self.state!=TFOOTER_STATE_FINISH)
        {
            _title.text=_infoPushing;
        }
    }
    
    //上推动画
    if(_isAnimationPushingActAtRelease)//上推到底时执行动画
    {
        if(progress>=1.0)//上推到底
        {
            if(self.state==TFOOTER_STATE_PUSHING)
            {
                [_indicatorPushing clearAnimations];
                
                NSMutableArray *animations=[NSMutableArray arrayWithCapacity:10];
                
                if(_animationPushing/ST_FOOTER_ANIM_ROTATE%10>0)//旋转动画
                {
                    [animations addObject:[_indicatorPushing animationRotationZToAngle:_animationPushingRotateEndValue*M_PI*2 withDuration:_animationPushingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPushing/ST_FOOTER_ANIM_SCALE_X%10>0)//X轴缩放动画
                {
                    [animations addObject:[_indicatorPushing animationScaleXToSize:_animationPushingScaleXEndValue withDuration:_animationPushingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPushing/ST_FOOTER_ANIM_SCALE_Y%10>0)//Y轴缩放动画
                {
                    [animations addObject:[_indicatorPushing animationScaleYToSize:_animationPushingScaleYEndValue withDuration:_animationPushingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPushing/ST_FOOTER_ANIM_STROKE_START%10>0)//初始点动画
                {
                    [animations addObject:[_indicatorPushing animationStrokeStartToValue:_animationPushingStrokeStartEndValue withDuration:_animationPushingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPushing/ST_FOOTER_ANIM_STROKE_END%10>0)//结束点动画
                {
                    [animations addObject:[_indicatorPushing animationStrokeEndToValue:_animationPushingStrokeEndEndValue withDuration:_animationPushingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPushing/ST_FOOTER_ANIM_ALPHA%10>0)//透明度动画
                {
                    [animations addObject:[_indicatorPushing animationToOpacity:_animationPushingAlphaEndValue withDuration:_animationPushingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                [_indicatorPushing startAnimation:[_indicatorPushing animationWithGroup:animations duration:_animationPushingDuration repeat:1 autoreverses:NO delay:0] andDelay:0];
            }
        }
        else//未上推到底
        {
            if(self.state==TFOOTER_STATE_RELEASE)
            {
                [_indicatorPushing clearAnimations];
                
                NSMutableArray *animations=[NSMutableArray arrayWithCapacity:10];
                
                if(_animationPushing/ST_FOOTER_ANIM_ROTATE%10>0)//旋转动画
                {
                    [animations addObject:[_indicatorPushing animationRotationZToAngle:_animationPushingRotateStartValue*M_PI*2 withDuration:_animationPushingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPushing/ST_FOOTER_ANIM_SCALE_X%10>0)//X轴缩放动画
                {
                    [animations addObject:[_indicatorPushing animationScaleXToSize:_animationPushingScaleXStartValue withDuration:_animationPushingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPushing/ST_FOOTER_ANIM_SCALE_Y%10>0)//Y轴缩放动画
                {
                    [animations addObject:[_indicatorPushing animationScaleYToSize:_animationPushingScaleYStartValue withDuration:_animationPushingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPushing/ST_FOOTER_ANIM_STROKE_START%10>0)//初始点动画
                {
                    [animations addObject:[_indicatorPushing animationStrokeStartToValue:_animationPushingStrokeStartStartValue withDuration:_animationPushingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPushing/ST_FOOTER_ANIM_STROKE_END%10>0)//结束点动画
                {
                    [animations addObject:[_indicatorPushing animationStrokeEndToValue:_animationPushingStrokeEndStartValue withDuration:_animationPushingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                if(_animationPushing/ST_FOOTER_ANIM_ALPHA%10>0)//透明度动画
                {
                    [animations addObject:[_indicatorPushing animationToOpacity:_animationPushingAlphaStartValue withDuration:_animationPushingDuration repeat:1 autoreverses:NO delay:0]];
                }
                
                [_indicatorPushing startAnimation:[_indicatorPushing animationWithGroup:animations duration:_animationPushingDuration repeat:1 autoreverses:NO delay:0] andDelay:0];
            }
        }
    }
    else
    {
        if(_animationPushing/ST_FOOTER_ANIM_ROTATE%10>0)//旋转动画
        {
            [_indicatorPushing transformRotateToAngle:_animationPushingRotateStartValue*M_PI*2+progress*(_animationPushingRotateEndValue-_animationPushingRotateStartValue)*M_PI*2];
        }
        
        if(_animationPushing/ST_FOOTER_ANIM_SCALE_X%10>0 || _animationPushing/ST_FOOTER_ANIM_SCALE_Y%10>0)//缩放动画
        {
            float w=(_animationPushing/ST_FOOTER_ANIM_SCALE_X%10>0?_animationPushingScaleXStartValue+progress*(_animationPushingScaleXEndValue-_animationPushingScaleXStartValue):1.0);
            float h=(_animationPushing/ST_FOOTER_ANIM_SCALE_Y%10>0?_animationPushingScaleYStartValue+progress*(_animationPushingScaleYEndValue-_animationPushingScaleYStartValue):1.0);
            
            [_indicatorPushing transformScaleToWitdh:w andHeight:h];
        }
        
        if(_animationPushing/ST_FOOTER_ANIM_STROKE_START%10>0)//初始点动画
        {
            _indicatorPushing.strokeStart=progress;
        }
        
        if(_animationPushing/ST_FOOTER_ANIM_STROKE_END%10>0)//结束点动画
        {
            _indicatorPushing.strokeEnd=progress;
        }
        
        if(_animationPushing/ST_FOOTER_ANIM_ALPHA%10>0)//透明度动画
        {
            _indicatorPushing.opacity=progress;
        }
    }
}


//上推完成
- (void)footerFinishPush
{
    [self footerPushWithProgress:1.0f];
}


//开始加载
- (void)footerStartLoad
{
    self.state=TFOOTER_STATE_LOADING;
    
    [self restoreIndicators];
    
    if(_hasLoadingIndicator)
    {
        _indicatorLoading.hidden=NO;
    }
    
    _title.text=_infoLoading;
    
    //动画
    NSMutableArray *animations=[NSMutableArray arrayWithCapacity:10];
    
    if(_animationLoading/ST_FOOTER_ANIM_ROTATE%10>0)//旋转动画
    {
        [animations addObject:[_indicatorLoading animationRotationZToAngle:_animationLoadingRotateEndValue*M_PI*2 withDuration:_animationLoadingDuration repeat:1 autoreverses:NO delay:0]];
    }
    
    if(_animationLoading/ST_FOOTER_ANIM_SCALE_X%10>0)//X轴缩放动画
    {
        [animations addObject:[_indicatorLoading animationScaleXToSize:_animationLoadingScaleXEndValue withDuration:_animationLoadingDuration repeat:1 autoreverses:NO delay:0]];
    }
    
    if(_animationLoading/ST_FOOTER_ANIM_SCALE_Y%10>0)//Y轴缩放动画
    {
        [animations addObject:[_indicatorLoading animationScaleYToSize:_animationLoadingScaleYEndValue withDuration:_animationLoadingDuration repeat:1 autoreverses:NO delay:0]];
    }
    
    if(_animationLoading/ST_FOOTER_ANIM_STROKE_START%10>0)//初始点动画
    {
        [animations addObject:[_indicatorLoading animationStrokeStartToValue:_animationLoadingStrokeStartEndValue withDuration:_animationLoadingDuration repeat:1 autoreverses:NO delay:0]];
    }
    
    if(_animationLoading/ST_FOOTER_ANIM_STROKE_END%10>0)//结束点动画
    {
        [animations addObject:[_indicatorLoading animationStrokeEndToValue:_animationLoadingStrokeEndEndValue withDuration:_animationLoadingDuration repeat:1 autoreverses:NO delay:0]];
    }
    
    if(_animationLoading/ST_FOOTER_ANIM_ALPHA%10>0)//透明度动画
    {
        [animations addObject:[_indicatorLoading animationToOpacity:_animationLoadingAlphaEndValue withDuration:_animationLoadingDuration repeat:1 autoreverses:NO delay:0]];
    }
    
    [_indicatorLoading startAnimation:[_indicatorLoading animationWithGroup:animations duration:_animationLoadingDuration repeat:-1 autoreverses:NO delay:0] andDelay:0];
}


//加载完成
- (void)footerFinishLoad
{
    
}


//加载完成后恢复
- (void)footerRestoreAfterLoad
{
    [self restoreIndicators];
    
    if(_hasPushingIndicator)
    {
        _indicatorPushing.hidden=NO;
    }
    
    _title.text=_infoPushing;
}


//释放内存
- (void)dealloc
{
    [_title release];
    [_indicatorPushing release];
    [_indicatorLoading release];
    [_infoPushing release];
    [_infoRelease release];
    [_infoLoading release];
    [_tips release];
    
    [super dealloc];
}


@end
